#!/usr/bin/python
###############################################################################
#Programmers:                                                                 #
#             Danil Dotsenko, dd@accentsolution.com                           #
#Date: 07/29/2006                                                             #
#Filename: renderer.py                                                        #
#Version: 0.1                                                                 #
#                                                                             #
#                                                                             #
#       Copyleft 2006 Danil Dotsenko                                          #
#                                                                             #
#    This program is free software; you can redistribute it and/or modify     #
#    it under the terms of the GNU General Public License as published by     #
#    the Free Software Foundation; either version 2 of the License, or        #
#    (at your option) any later version.                                      #
#                                                                             #
#    This program is distributed in the hope that it will be useful,          #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of           #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            #
#    GNU General Public License for more details.                             #
#                                                                             #
#    You should have received a copy of the GNU General Public License        #
#    along with this program; if not, write to the Free Software              #
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA #
#                                                                             #
###############################################################################

'''
SuperKaramba-specific image manipulation and rendering collection.
Primary purpose is to serve miloCalendar main code
 by abstracting karamba.draw operations.

Notes:
-
'''

import canvasclasses

# the errors on missing sections in dictionaries are DEMN ANNOYING! JUst return None demn you!
# and I am really pissed off about using TRY all over the place.
def safeget(key, dictionary):
	output = None
	if dictionary.has_key(key):
		output = dictionary[key]
	return output

def prepareCanvasElement(section, settings):
	# this is the SELECTOR of SUpported sections. Use the one you like most.

	#srchMask={}
	#sections = ['Background', 'DaysWeekend', 'Days', 'Today', 'Weekdays', 'Month', 'Year', 'WeekNumbers', 'Event']
	#srchMask['normalSections'] = re.compile('|'.join(sections)) # you get something like 'asdf|zxcv|qwer' as search criteria
	#sections = ['Button.+', 'Image.+']
	#srchMask['activeElements'] = re.compile('|'.join(sections)) # you get something like 'asdf|zxcv|qwer' as search criteria
	#nowServing = 0

	#if srchMask['normalSections'].match(section):
		# ok, we can do this.
	command = 'names, settings = prepare'+section+'('+unicode(settings)+')'
	exec command
	return names, settings

def prepareBackground(settings):

	# Name for element index
	name = 'Background'

	# this will go into canvas array.
	names = [name] # the only instance, so in index it will be just that - Background

	# this will go into inventory dictionary.
	newset = {}
	newset[name] = {}
	newset[name]['Enable'] = 0
	newset[name]['Rasterizer'] = None
	newset[name]['fileName'] = None
	newset[name]['fileNameRange'] = None # it will remain None. Test for that before use.

	'''
	Mode = 0
	SolidColor = 8080
	Bevel = 1
	BitmapName = back.png
	'''
	# BackgroundMode
	# 0 = Tile background to fill the whole window
	# 1 = Transparent background
	# 2 = Stretch background to fill the whole window
	# 3 = Solid color background
	#     For simplicity we will treat 3 as 1, and 2 as 0
	value = safeget('Mode',settings)
	if value:
		value = int(value)
		if value == 2 or value == 0:
			newset[name]['fileName'] = safeget('BitmapName',settings)
			newset[name]['Rasterizer'] = 'BITMAP'
			newset[name]['Enable'] = 1
	return names, newset

def prepareMonth(settings):

	# Name for element index
	name = 'MonthTitle'

	# this will go into canvas array.
	names = [name] # the only instance, so in index it will be just that - Background

	# this will go into inventory dictionary.
	newset = {}
	newset[name] = {}
	newset[name]['Enable'] = 0
	newset[name]['Rasterizer'] = None
	newset[name]['fileName'] = None
	newset[name]['fileNameRange'] = None

	'''
	Enable = 1
	Align = 33
	Names = January/February/March/April/May/June/July/August/September/October/November/December
	FontColor = 969696
	Y = 145
	X = 207
	Font = -11/0/0/0/700/0/0/0/0/3/2/1/34/Arial
	BitmapName = month.png
	Rasterizer = BITMAP
	'''
	value = safeget('Enable',settings)
	if value == '1':
		newset[name]['Rasterizer'] = safeget('Rasterizer',settings)
		if newset[name]['Rasterizer'] == 'BITMAP':
			newset[name]['fileName'] = safeget('BitmapName',settings)
			newset[name]['fileNameRange'] = range(12)
			newset[name]['Enable'] = 1
	return names, newset


def prepareYear(settings):

	# Name for element index
	name = 'Year'

	# this will go into canvas array.
	names = [name] # the only instance, so in index it will be just that - Background

	# this will go into inventory dictionary.
	newset = {}
	newset[name] = {}
	newset[name]['Enable'] = 0
	newset[name]['Rasterizer'] = None
	newset[name]['fileName'] = None
	newset[name]['fileNameRange'] = None

	'''
	Enable = 0
	BitmapName = ""
	Rasterizer = FONT
	Separation = -4
	X = 133
	Y = 19
	FontColor = A3A3A3
	Font = -11/0/0/0/700/0/0/0/0/3/2/1/34/Arial
	Align = 0
	'''
	value = safeget('Enable',settings)
	if value:
		newset[name]['Rasterizer'] = safeget('Rasterizer',settings)
		if newset[name]['Rasterizer'] == 'FONT':
			newset[name]['fileName'] = safeget('BitmapName',settings)
			newset[name]['fileNameRange'] = range(12)
			newset[name]['Enable'] = 1
	return names, newset

#####################################################################
# Testing, testing, 1,2,3...

def test():
	renderOrder = ['Background', 'Month', 'Year'] # 'Days','Today','Event'
	print renderOrder

	canvas = []
	inventory = {}

	import localconf
	cfg = localconf.cfgObject('converted.ini')
	for section in renderOrder:
		settings = cfg.get(section)
		print 'Starting section ', section, 'and settings of ', type(settings), '\n', settings
		names, settings = prepareCanvasElement(section, settings)
		# where names MUST be array, and
		# settings MUST be dict with {name1:{'setting1':setting,'setting2':setting},name2:{},...}
		canvas.extend(names)
		inventory.update(settings)
	cfg = None
	print canvas
	print inventory

if __name__ == '__main__':
	test()